package com.game.manager;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Observable;

import org.xsocket.connection.INonBlockingConnection;

import com.command.Group;
import com.connect.agreement.Packet;
import com.game.base.CommObserver;
import com.game.performers.Actor;
import com.module.itemeditor.EditorManager;
import com.module.login.LoginManager;
import com.module.mail.MailManager;
import com.module.regrole.RegRoleManager;
import com.module.union.UnionManager;
import com.server.User;

public class CommManager {
	private static CommManager instance;
	public static CommManager get(){
		if(instance==null){
			instance = new CommManager();
		}
		return instance;
	}
	public final HashMap<Integer,INonBlockingConnection> conns = new HashMap<Integer, INonBlockingConnection>();
	public final Hashtable<String,Actor> roles = new Hashtable<String, Actor>();
	
	public CommManager(){
		instance = this;
	}
	
	public boolean addCommObserver(CommObserver observer){
		return false;
	}
	public void deleteCommObsever(CommObserver observer){
		
	}
	public void broadcastChanges(){
		
	}
	public void broadcastNewObjectMessage(int roleid,String classname){
		INonBlockingConnection conn = conns.get(roleid);
		if(conn != null){
			
		}
	}
	public void broadcastModifyObjectMessage(int roleid,Packet packet){
		INonBlockingConnection conn = conns.get(roleid);
		if(conn != null){
			try {
				conn.write(packet.byteBuffer());	
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	public void broadcastDeleteObjectMessage(int roleid){
		INonBlockingConnection conn = conns.get(roleid);
		if(conn != null){
			
		}
	}
	public void sendNewObjectMessage(INonBlockingConnection nbc,int id, String classname){
		
	}
	public void sendModifyObjectMessage(INonBlockingConnection nbc,int id,Packet packet){
		
	}
	public void sendDeleteObjectMessage(INonBlockingConnection nbc, int id){
		
	}
	public INonBlockingConnection getConn(int roleid){
		return conns.get(roleid);
	}
	public void subscribe(INonBlockingConnection nbc, Actor role){
		if(!conns.containsKey(nbc.getId())){
			roles.put(nbc.getId(), role);
			conns.put(role.id, nbc);
			System.out.println(role.getName()+" 被控制，上线！");
		}
	}
	public void unSubscribe(INonBlockingConnection nbc){
		Actor role = roles.get(nbc.getId());
		if(role!=null)
		{
			conns.remove(role.id);
			roles.remove(nbc.getId());
			System.out.println(role.getName() + " 脱离控制，下线！");
		}
	}
	public void update(INonBlockingConnection nbc, Packet packet) {
		System.out.println(packet);
		switch(packet.getGroupcmd()){
		case Group.Login:
			LoginManager.update(nbc,packet);
			break;
		case Group.RegRole:
			RegRoleManager.update(nbc, packet);
			break;
		case Group.Editor:
			EditorManager.update(nbc, packet);
			break;
		case Group.Mail:
			MailManager.update(nbc, packet);
			break;
		case Group.Union:
			UnionManager.update(nbc, packet);
			break;
		}
	}
}
